package com.qx.ddx.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qx.ddx.DDXApplication;
import com.qx.ddx.R;
import com.qx.ddx.activity.MainActivity;
import com.qx.ddx.activity.fragment.adapter.LanguageAdapter;
import com.qx.ddx.util.PreferenceUtil;
import com.qx.ddx.util.SpInfo;
import com.qx.ddx.view.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2016/6/24.
 */
public class LanguageFragment extends Fragment {
    private RecyclerView reView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ContentView = inflater.inflate(R.layout.fragment_set_language, container, false);
        reView = (RecyclerView) ContentView.findViewById(R.id.set_language_recyclerView);
        LanguageAdapter adapter = new LanguageAdapter();
        adapter.setData(getData(adapter));
        adapter.setOnItemClickListener(new LanguageAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int index) {
                SharedPreferences sp = getActivity().getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt(SpInfo.LanguageIndex, index);
                editor.commit();
                switch (index) {
                    case 0:
                        switchLanguage("zh");
                        DDXApplication.getInstance().quit();
                        getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        switchLanguage("en");
                        DDXApplication.getInstance().quit();
                        getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
                        break;
                }
            }
        });
        reView.setAdapter(adapter);
        reView.setHasFixedSize(false);
        reView.setLayoutManager(new LinearLayoutManager(getActivity()));
        reView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
//        reView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        return ContentView;
    }

    protected void switchLanguage(String language) {
        //璁剧疆搴旂敤璇█绫诲瀷
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        if (language.equals("en")) {
            config.locale = Locale.ENGLISH;
        } else {
            config.locale = Locale.SIMPLIFIED_CHINESE;
        }
        resources.updateConfiguration(config, dm);
        PreferenceUtil.commitString("language", language);
    }

    private List<HashMap<String, String>> getData(LanguageAdapter adapter) {
        List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        String[] names = new String[]{"中文（简体）", "中文（台湾繁体）", "中文（香港繁体）", "English（United States）"};
        SharedPreferences sp = getActivity().getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
        int index = sp.getInt(SpInfo.LanguageIndex, 0);
        adapter.setSelectedIndex(index);
        for (int i = 0; i < names.length; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", names[i]);
            if (index == i) {
                map.put("isSelected", "true");
            } else {
                map.put("isSelected", "false");
            }
            data.add(map);
        }
        return data;
    }
}
